package cn.wildfire.chat.app.extendsion.classes;

import android.content.Context;
import android.util.Log;

import org.json.JSONObject;

import java.util.HashMap;

import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;

/**
 * Create by HEKE, 2019/10/17 10:23
 * TODO 绑定手机号请求
 */
public class BindPhoneRequest {

    private Context context;
    private String phone;
    private String uid;
    private String regionId;
    private String code;

    private OnBindPhoneListener onBindPhoneListener;

    public BindPhoneRequest(Context context, String phone, String uid, String regionId, String code) {
        this.context = context;
        this.phone = phone;
        this.uid = uid;
        this.regionId = regionId;
        this.code = code;
        req();
    }

    public BindPhoneRequest(Context context, String phone, String uid, String regionId, String code, OnBindPhoneListener onBindPhoneListener) {
        this.context = context;
        this.phone = phone;
        this.uid = uid;
        this.regionId = regionId;
        this.code = code;
        this.onBindPhoneListener = onBindPhoneListener;
        req();
    }

    protected void req() {
        try {
            HashMap<String, Object> params = new HashMap<>();
            params.put("mobile", phone);
            params.put("uid", uid);
            params.put("regionId", regionId);
            params.put("code", code);
            OkHttpRequest.getNewInstance(context).requestAsynJsonObject(ServerUrlClient.BIND_PHONE, params, new ReqCallBack<String>() {
                @Override
                public void onReqSuccess(String result) {
                    try {
                        if (!TextUtils.isTextEmpty(result)) {
                            ResponseEntity<JSONObject, BindUserInfo> responseEntity = new ResponseEntity<>(result, BindUserInfo.class);
                            if (responseEntity.isSuccess()) {
                                if (onBindPhoneListener != null) {
                                    BindUserInfo bindUserInfo = responseEntity.parseResult();
                                    onBindPhoneListener.bindPhone(true, bindUserInfo, responseEntity.message);
                                }
                            } else {
                                if (onBindPhoneListener != null) {
                                    onBindPhoneListener.bindPhone(false, null, responseEntity.message);
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onReqFailed(String errorMsg) {
                    Log.e("#### BindPhoneRequest", " errorMsg : " + errorMsg);
                    if (onBindPhoneListener != null) {
                        onBindPhoneListener.bindPhone(false, null, errorMsg);
                    }
                }
            }).request();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create by HEKE, 2019/10/17 10:38
     * TODO 绑定手机回调接口
     */
    public interface OnBindPhoneListener {
        void bindPhone(boolean success, BindUserInfo bindUserInfo, String message);
    }

}
